import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class Main {
    public static void main(String[] args) {
        JFrame window =new JFrame();
        window.setLayout(new FlowLayout());
        window.setVisible(true);
        window.setSize(600,400);
        window.setTitle("货款计算器");

        JLabel labelAmout =new JLabel("货款总额");
        JLabel labelPeriod =new JLabel("货款期数");
        JLabel labelInterest =new JLabel("货款年利率");

        JTextField textAmout= new JTextField();
        JTextField textPeriod= new JTextField();
        JTextField textInterest= new JTextField();
        textAmout.setColumns(10);
        textPeriod.setColumns(10);
        textInterest.setColumns(10);


        JRadioButton radioAvgCaption =new JRadioButton("等额本金");
        JRadioButton radioAVgInterest =new JRadioButton("等额本息");

        ButtonGroup buttonGroup =new ButtonGroup();
        buttonGroup.add(radioAvgCaption);
        buttonGroup.add(radioAVgInterest);

        JLabel labelTotalInterest =new JLabel("总利息");
        JTextField textTotalInterest= new JTextField();
        textTotalInterest.setColumns(20);

        JButton button =new JButton();
        button.setLabel("计算");
        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.读取用户的输入
                int amout =Integer.parseInt(textAmout.getText());
                int period =Integer.parseInt(textAmout.getText());
                double yearInterest  =Double.parseDouble(textInterest.getText());
                //2.计算
//               LoanCalculator loanCalculator =new LoanCalculator(amout,period,yearInterest);
                Rate loanCalculator =new Rate(amout,period,yearInterest);
                double result =0.0;
                if (radioAvgCaption.isSelected()){
                    result = loanCalculator.Benxi();
                }else {
                    result =loanCalculator.BenJin();
                }
                textTotalInterest.setText(String.valueOf(result));
                //3.输出
            }
        });

        window.add(labelAmout);
        window.add(textAmout);
        window.add(labelPeriod);
        window.add(textPeriod);
        window.add(labelInterest);
        window.add(textInterest);
        window.add(radioAvgCaption);
        window.add(radioAVgInterest);
        window.add(labelTotalInterest);
        window.add(textTotalInterest);
        window.add(button);


        window.setVisible(true);


    }
}